package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    public List<Map<String, Object>> getMenuByUsername1(String username) {
        List<Menu> menus = menuDao.getMenuByUsername1(username);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Menu menu : menus) {
            List<Menu> children = menu.getChildren();
            Map<String, Object> parentMap = new HashMap<>();
            if (children != null) {
                parentMap.put("path", menu.getPath());
                parentMap.put("title", menu.getName());
                parentMap.put("icon", menu.getIcon());
                List<Map<String, String>> childrens = new ArrayList<>();
                for (Menu child : children) {
                    Map<String, String> childrenMap = new HashMap<>();
                    childrenMap.put("path", child.getPath());
                    childrenMap.put("title", child.getName());
                    childrenMap.put("linkUrl", child.getLinkUrl());
                    childrens.add(childrenMap);
                }
                parentMap.put("children", childrens);
            }
            result.add(parentMap);
        }
        return result;
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List findAll() {
        return menuDao.findAll();
    }

    @Override
    public void add(Menu menu, Integer parentMenuId) {
        addOrEdit(menu, parentMenuId);
        menuDao.add(menu);
    }

    private void addOrEdit(Menu menu, Integer parentMenuId) {
        if (parentMenuId==null){
            Integer priority = menu.getPriority();
            menu.setPath(String.valueOf(priority+1));
            menu.setLevel(1);
        }
        if (parentMenuId!=null) {
            menu.setParentMenuId(parentMenuId);
            Menu menu1 = menuDao.findMenuByParentId(parentMenuId);
            String path = menu1.getPath();
            Integer priority = menu.getPriority();
            menu.setPath("/"+path+"-"+priority);
            menu.setLevel(2);
        }
    }

    @Override
    public Integer findParentMenuIdByMenuId(Integer menuId) {
        return menuDao.findParentMenuIdByMenuId(menuId);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu, Integer parentMenuId) {
        addOrEdit(menu,parentMenuId);
        menuDao.edit(menu);
    }

    @Override
    public void deleteById(Integer id) {
        menuDao.deleteById(id);
    }
}
